package testing;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import inventory.daoImplementer.CategoryMasterImplementor;
import dbhandelling.Connections;
import dbhandelling.Crude;

public class Test {

	/**
	 * @param args
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public static void main(String[] args) throws InstantiationException, IllegalAccessException {
		
		
		CategoryMasterImplementor catm=new CategoryMasterImplementor();
		
		Class t = null;
		try {
			t = Class.forName("inventory.model.Gin");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Object a=t.newInstance();
		
		
			//System.out.println(m);
		Field[] f=t.getDeclaredFields();
		//for(Field temp:f)
			//System.out.println(temp.getName());
		
		Method[] methods=t.getMethods();
		Method[] setter=null;
		int count=0;
		
		for(Method temp:methods)
		   if(isSetter(temp))
			   count++;
		
		setter=new Method[count];
		
		int i=0;
		for(Method temp:methods)
			   if(isSetter(temp))
			   {
				   
				   setter[i]=temp;
				   i++;
			   }
		
		HashMap<Field, Method> hm=new HashMap<Field, Method>();
		String tempField,tempMethod;
		
		for( i=0;i<f.length;i++)
		{
			
			for(int j=0;j<setter.length;j++)
			{
				
				tempField=f[i].getName().toLowerCase();
				tempMethod=setter[j].getName().toLowerCase();
				
				if(tempMethod.endsWith(tempField))
				{
					
					hm.put(f[i], setter[j]);
				}
			}
		}
			
		Set set = hm.entrySet();
	      // Get an iterator
	      Iterator k = set.iterator();
	      // Display elements
	      while(k.hasNext()) {
	         Map.Entry me = (Map.Entry)k.next();
	         System.out.print(me.getKey() + ": ");
	         System.out.println(me.getValue());
	      }
		

	}
	
	public static boolean isGetter(Method method){
		  if(method.getName().startsWith("get")) return true;
		  if(method.getParameterTypes().length != 0)   return false;  
		  if(void.class.equals(method.getReturnType())) return false;
		      return true;
		}
	
	public static boolean isSetter(Method method){
		  if(!method.getName().startsWith("set")) return false;
		  if(method.getParameterTypes().length != 1) return false;
		  return true;
		}
	
	private HashMap<Field,Method> getFieldMethodPair(String classname) throws Exception
	{
		Class t = Class.forName("inventory.model.Gin");
		Field[] f=t.getDeclaredFields();
		Method[] methods=t.getMethods();
		Method[] setter=null;
		int count=0;
		for(Method temp:methods)
		{
		   if(isSetter(temp))
			   count++;
		}
		setter=new Method[count];
		int i=0;
		for(Method temp:methods)
			   if(isSetter(temp))
			   {
				   
				   setter[i]=temp;
				   i++;
			   }
		
		HashMap<Field, Method> hm=new HashMap<Field, Method>();
		String tempField,tempMethod;
		
		for( i=0;i<f.length;i++)
		{
			
			for(int j=0;j<setter.length;j++)
			{
				
				tempField=f[i].getName().toLowerCase();
				tempMethod=setter[j].getName().toLowerCase();
				
				if(tempMethod.endsWith(tempField))
				{
					
					hm.put(f[i], setter[j]);
				}
			}
		}
		return hm;
	}

}
